package com.cn.process;

import com.cn.common.HttpCommon;
import com.cn.enums.TypeBodyEnum;
import com.cn.http.HttpParam;
import com.cn.http.HttpRequest;
import com.cn.http.HttpResponse;
import com.cn.http.impl.FileUpload;
import com.cn.http.impl.Http;
import com.cn.http.impl.SimpleHttpParam;
import com.cn.http.impl.SimpleHttpRequest;
import com.cn.utils.MapUtils;

import java.io.IOException;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;

/**
 * &#064;Time 2024 八月 星期六 14:56
 *
 * @author ShangGuan
 */
public class HttpUtils {


    public static HttpResponse get(String url) {
        return get(url, new HashMap<>());

    }

    public static HttpResponse get(String url, Map<String, Object> form) {
        return get(url, new HashMap<>(), form);
    }

    public static HttpResponse get(Map<String, String> header, String url) {
        return get(url, header, new HashMap<>());
    }

    public static HttpResponse get(String url, Map<String, String> headers, Map<String, Object> form) {
        try {
            Http http = new Http(new URL(url));
            HttpParam simpleHttpParam = new SimpleHttpParam();
            if (!MapUtils.isEmpty(new HashMap<>(headers))) {
                http.setHeaders(headers);
            }
            if (!MapUtils.isEmpty(new HashMap<>(form))) {
                simpleHttpParam.setForm(form);
            }
            HttpRequest httpRequest = new SimpleHttpRequest(http);
            httpRequest.setRequestParams(simpleHttpParam);
            return httpRequest.get();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    public static HttpResponse post(String url) {
        return post(url, new HashMap<>());
    }

    public static HttpResponse post(String url, FileUpload fileUpload) {
        return post(url,new HashMap<>(), fileUpload);
    }

    public static HttpResponse post(String url, Map<String,Object> params,FileUpload fileUpload) {
        return post(url, new HashMap<>(),params, fileUpload);
    }


    /**
     * 发送文件格式的请求
     *
     * @param url        url
     * @param headers    标题
     * @param params     参数
     * @param fileUpload 文件上传
     * @return {@link HttpResponse }
     */
    public static HttpResponse post(String url, Map<String, String> headers,Map<String,Object> params, FileUpload fileUpload) {
        try {
            Http http = new Http(new URL(url));
            HttpParam simpleHttpParam = new SimpleHttpParam();
            simpleHttpParam.setType(TypeBodyEnum.TYPE_MULTIPART_FORM_DATA);
            simpleHttpParam.setFileUpload(fileUpload);
            if (!MapUtils.isEmpty(new HashMap<>(headers))) {
                http.setHeaders(headers);
            }
            if (!MapUtils.isEmpty(new HashMap<>(params))) {
                simpleHttpParam.setBody(params);
            }
            http.addRequestHeader(HttpCommon.CONTENT_TYPE, simpleHttpParam.getType().getHeader());
            HttpRequest httpRequest = new SimpleHttpRequest(http);
            httpRequest.enableFileUpload();
            httpRequest.setRequestParams(simpleHttpParam);
            return httpRequest.post();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    public static HttpResponse post(String url, Map<String, Object> body) {
        return post(url, new HashMap<>(), body);
    }

    public static HttpResponse post(Map<String, String> headers, String url) {
        return post(url, headers, new HashMap<>());
    }

    public static HttpResponse post(String url, Map<String, String> headers, Map<String, Object> body) {
        try {
            Http http = new Http(new URL(url));
            HttpParam simpleHttpParam = new SimpleHttpParam();
            if (!MapUtils.isEmpty(new HashMap<>(headers))) {
                http.setHeaders(headers);
            }
            if (!MapUtils.isEmpty(new HashMap<>(body))) {
                simpleHttpParam.setType(TypeBodyEnum.TYPE_APPLICATION_JSON);
                simpleHttpParam.setBody(body);
                http.addRequestHeader(HttpCommon.CONTENT_TYPE,simpleHttpParam.getType().getHeader());
            }
            HttpRequest httpRequest = new SimpleHttpRequest(http);
            httpRequest.setRequestParams(simpleHttpParam);
            return httpRequest.post();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }


}
